package org.example.com.atguigu.day05;

import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.expressions.UserDefinedFunction;

import static org.apache.spark.sql.functions.udaf;

public class UserDefinedUDAF {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().master("local[4]").appName("test").getOrCreate();
        spark.read()
                .option("sep","\t")
                .csv("E:\\IdeaProject\\bigdata0228\\spark0228\\datas\\student.csv")
                .toDF("stu_name","name","score")
                .createOrReplaceTempView("student");
        // 创建自定义对象
        AvgAgg avgAgg = new AvgAgg();
        // 转换类型
        UserDefinedFunction func = udaf(avgAgg, Encoders.INT());
        // 注册
        spark.udf().register("muAVG", func);
        spark.sql("select name,muAVG(score) from student group by name").show();

    }

}
